<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;
use App\Captcha;
use App\CaptchaData;


use function PHPSTORM_META\map;

class securityCode extends Controller
{
    //获取用户的安全码，
    public function getM(Request $req){
        $user_id = $req->user_id;
        if(!$user_id){
            return ['code'=>-1,'message'=>'参数缺失'];
        }
        $key = Cookie::get('key');
        if(empty($key) || $key!=$user_id){
            //验证不通过，越权了，让客户端重新登录
            return ['code'=>-2,'message'=>'您越权了'];
        }
        //获取用户详细信息
        $user = DB::table('user')->where('id',$user_id)->first(['id','account','email']);
        //获取数据
        $data = DB::table('captcha')->where('user_id',$user->id)->get(['id','code','create_time','title','user_id']);
        
        foreach($data as $a){
            $a->option = DB::table('captcha_data')->where('captcha_id',$a->id)->get(['id','captcha_id','url','port','create_time']);
        }
        return ['code'=>200,'message'=>'获取成功','data'=>['list'=>$data,'user'=>$user]];

    }


    /**
     *   code: "541"
     *   create_time: "2021-04-06 21:52:50"
     *   id: 1
     *   option: Array(3)
     *    0: {id: 1, captcha_id: 1, url: "https://www.baidu.com", port: "443", create_time: "2021-04-06 22:46:55", …}
     *    1: {id: 2, captcha_id: 1, url: "https://www.baidu.com", port: "443", create_time: "2021-04-06 22:46:55", …}
     *    2: {id: 3, captcha_id: 1, url: "https://www.baidu.com", port: "443", create_time: "2021-04-06 22:46:55", …}
     *   length: 3
     *   __proto__: Array(0)
     *   title: "百度"
     *   update_time: "2021-04-06 22:51:40"
     *   user_id: "baige"
     */
    //更新安全码
    public function updateM(Request $req){
        //修改选项
        $captcha = $req->captcha;
        $option = $captcha['option'];
        unset($captcha['option']);
        //删除的选项
        $delect = $req->delect;
        $user_id = $req->user_id;

        //执行删除
        foreach($delect as $a){
            DB::table('captcha_data')->where('id',$a)->delete();
        }
        //执行修改主体,如果获取失败则创建一条记录
        if(empty($captcha['id'])){
           //添加主体新数据返回id
           $id = DB::table('captcha')->insertGetId($captcha);
        }else{
            //修改
            $id = $captcha['id'];
            $a = Captcha::where('id',$id)->first();
            $a->code = $captcha['code'];
            $a->title = $captcha['title'];
            $a->save();
        }

        /***
         *   captcha_id: 1
         *   id: 1
         *   port: "80"
         *   url: "http://www.baidu.com"
         */
        //添加选项或修改选项
        foreach($option as $a){
            if($a['captcha_id']){
                //有id直接修改
                CaptchaData::where('id',$a['id'])->update($a);
            }else{
                //没有id说明是新数据添加
                $a['captcha_id'] = $id;
                if(!DB::table('captcha_data')->insertGetId($a)){
                    return ['code'=>-2,'message'=>'选项添加失败','data'=>$a];
                }
            }
            
        }
        $data=null;
        if(empty($captcha['id'])){
            $data = DB::table('captcha')->where('id',$id)->first();
            $data->option = DB::table('captcha_data')->where('captcha_id',$id)->get();
        }
        return ['code'=>200,'message'=>'添加成功','data'=>$data];
    }

    //删除安全码
    public function delectM(Request $req){
        $account = $req->userId;
        $delect_id = $req->delectId;

        if($account && $delect_id){
            foreach($delect_id as $a){
                Captcha::where('id',$a)->delete();
                CaptchaData::where('captcha_id',$a)->delete();
            }
            return ['code'=>200,'message'=>'删除成功'];
        }else{
            return ['code'=>-1,'message'=>'参数错误'];
        }

    }




}
